/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/
#ifndef _HTR_CLIENT_HEADER_
#define _HTR_CLIENT_HEADER_

/********************************************************************\
                            Includes
\********************************************************************/
#include <QObject>
#include <QString>
#include <QTimer>
#include "htr/htrglobal.h"
#include "htr/mansock.h"

/********************************************************************\
                          Declaration
\********************************************************************/
class HtrClient : public QObject
{
  Q_OBJECT
  public:
    HtrClient();
    virtual ~HtrClient();

    void  connectToHost(const QString &host, int port);
    bool  isReady();

    void setAutoConnect(bool);
    bool autoConnect() const;

    QDataStream &prepareStream(Htr::Net::ClMessageType t = Htr::Net::ClResourceMsg);
    void         writeStream();
    void         setupStream(QDataStream &s); // Aufsetzen der einheitlich eingesetzten Stream-Version


    typedef enum State_ {

      StOffline,
      StConnect,
      StConnecting,
      StSndWelcome,
      StRcvWelcome,
      StConnected
    } ClientState;

    ClientState state() const;

  signals:
    void stateChanged();
    void sysopMsg(const QString &shortMessage);

    // Signals an die Ressourcen:
    void urlIdent(const QString &url, qint32 ident, qint32 type);
    void resourceMsg(qint32 ident, QDataStream &msg);
    void monitorMsg(QDataStream &msg);

    // senden/Empfangen:
    void rx();
    void tx();

  public slots:
    void  close();
    void  reconnect();

  private slots:
    void handleMsg();
    void handleState();
    void handleSocketError();
    void keepAlive(); 

  private:
    void  setState(ClientState s);
    void  handleIdentMsg(QDataStream &inStream);
    void  handleWelcomeMsg(QDataStream &inStream);
    void  handleSmsMsg(QDataStream &inStream);
    void  handleResourceMsg(QDataStream &inStream);
    void  handleMonitorMsg(QDataStream &inStream);
    void  sendWelcomeMsg();

    HtrManagedSocket mSocket;
    ClientState      mState;
    QString          mNetHost;
    qint32           mNetPort;
    qint32           mClientNetProtocol;
    qint32           mServerNetProtocol;
    bool             mAutoConnect;
    QTimer           mAutoConnectTimer;
};

#endif 
